<script>
import { SetCookie } from '../../utils/cookie'
import { NANCH5COOKIEKEY } from '../../constants/index'
export default {
  async mounted() {
    const q = this.$route.query
    const code = q.code
    const from = q.from
    if (code) {
      console.log('code yes', code)
      const { data } = await this.$axios({
        url: '/wx/getUserInfo',
        method: 'POST',
        params: {
          code
        }
      })
      console.log('request data', data.data)
      if (!data || (data && data.data && data.data.code !== 200)) {
        this.$toast({
          message: '授权认证失败' + (data && data.msg),
          onClose: () => {
            window.location.href = from
          }
        })
      }
      const uuid = data.data.uuid
      if (!uuid) {
        this.$toast({
          message: '未获取到用户信息,请重试',
          onClose: () => {
            window.location.href = from
          }
        })
      } else {
        // 设置 uuid 到 cookie
        console.log('设置cookie', data)
        SetCookie(NANCH5COOKIEKEY, uuid, 365)
        this.$toast({
          message: '授权成功',
          onClose: () => {
            window.location.href = from
          }
        })
      }
    }
  }
}
</script>

<template>
  <div class="auth" />
</template>
<style scoped>
.auth {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
</style>
